package safeCommitter.core.view.main.sorter;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.TreeColumn;

import safeCommitter.test.report.ISCClassMethodTestResult;
import safeCommitter.test.report.SCClazzTestResult;

public class SorterManager {

	public void addSorter(final TreeViewer viewer, final TreeColumn[] columns) {
		new ColumnViewerSorter(viewer, columns[0]) {
			@Override
			protected int doCompare(final Viewer viewer, final Object e1,
					final Object e2) {
				if (e1 instanceof SCClazzTestResult
						&& e2 instanceof ISCClassMethodTestResult) {
					SCClazzTestResult m1 = (SCClazzTestResult) e1;
					SCClazzTestResult m2 = (SCClazzTestResult) e2;
					return m1.getClassName().compareToIgnoreCase(
							m2.getClassName());
				}
				return 0;
			}
		};

		new ColumnViewerSorter(viewer, columns[1]) {
			@Override
			protected int doCompare(final Viewer viewer, final Object e1,
					final Object e2) {
				if (e1 instanceof ISCClassMethodTestResult
						&& e2 instanceof ISCClassMethodTestResult) {
					ISCClassMethodTestResult m1 = (ISCClassMethodTestResult) e1;
					ISCClassMethodTestResult m2 = (ISCClassMethodTestResult) e2;
					return Integer.valueOf(m1.getTimeSpent()).compareTo(
							Integer.valueOf(m2.getTimeSpent()));
				}
				return 0;
			}
		};

	}

}
